﻿using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using SimpleSpreadsheet.Framework;
using SimpleSpreadsheet.ViewModels;

namespace SimpleSpreadsheet.Views
{
    /// <summary>
    /// Interaction logic for ShellView.xaml
    /// </summary>
    public partial class ShellView : Window
    {
        public ShellView()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var actualHeight = (int) ((Grid) Content).ActualHeight;
            var actualWidth = (int) ((Grid) Content).ActualWidth;
            var shellViewModel = ViewModelLocator.LocateForView(this) as ShellViewModel;
            if (shellViewModel != null)
                shellViewModel.ShowWorkbook(actualHeight/22 - 2, actualWidth/64 - 1);
        }

        private void Window_Closing(object sender, CancelEventArgs e)
        {
            var viewModel = ViewModelLocator.LocateForView(this) as ShellViewModel;

            if (viewModel != null)
                if (viewModel.CurrentWorkbookViewModel.CurrentWorksheetViewModel.Worksheet.IsChanged)
                {
                    MessageBoxResult result =
                        MessageBox.Show("Do you want to save the changes you made to the document?",
                                        "Simple Spreadsheet cares for you", MessageBoxButton.YesNoCancel,
                                        MessageBoxImage.Question);

                    e.Cancel = (result == MessageBoxResult.Cancel);
                    if (result == MessageBoxResult.Yes)
                        viewModel.CurrentWorkbookViewModel.SaveDocument();
                }
        }
    }
}